#include <vector>
#include <iostream>

#include "Util.H"
#include "IO.H"
#include "Image.H"
#include "eval_thresh.h"

using namespace std;
using namespace pip;

int main(int argc, char **argv)
{
	//see if arguments are passed
    if (argc < 4) {
	  error("", "Usage: optimal_threshold -i INPUT_IMAGE -o SEGMENTED_IMAGE [-m MASK_IMAGE]\n Creates binary output image using optimal thresholding on input image. \n If a mask is passed it is considered.");
 	  return 1;
    }
	//collect arguments
    vector<string> args(argv, argv+argc);
    string inp, outp, mask("");
	//init threshold
	float threshold(-9999); 
    //get operands
	getScalarArg(args, "-m"     , mask);
    getScalarArg(args, "-i"     , inp);
    getScalarArg(args, "-o"     , outp);
	//init images 
	Image<short>         input;
	Image<unsigned char> a_mask;
	//load images
	if( !mask.empty() && !importFile(a_mask, mask)){
		error("", "cannot import image");
 		return 1;
	}
	if(!importFile(input, inp)){
		error("", "cannot import image");
 		return 1;
	}
	// reserve output image of equal size
	Image<unsigned char> output(input.dimension());
	
	if(a_mask.dim(0)>0){
		optimal_thresholding(input, output, threshold,a_mask);  // call optimal threshold - with or without mask
	}else{
		optimal_thresholding(input, output, threshold);
	}
	
	if(!exportFile(output, outp)){            //save output
		error("", "cannot export image");
 		return 1;
	}
  system("pause");
}